var appAdminSettings = new Vue({
  el: "#app-admin-settings",
  data: {
    phone: 0,
    passwordStore: {
      originalPassword: '',
      newPassword: '',
      repeatPassword: ''
    },
    shouldAlert: false,
    errorMessage: ''
  },
  methods: {
    checkAdminPhoneInCookie: function() {
      //if (Cookies.get("admin_phone") === undefined) {
      if (window.localStorage.getItem("admin_phone") == undefined) {
        Ply.dialog('alert', '您未登录！').done(function() {
          window.location.href = '../login/login.html';
        });
      } else {
        // this.phone = Cookies.get("admin_phone");
        this.phone = window.localStorage.getItem("admin_phone");
        console.log(this.phone);
      }
    },
    changePassword: function(event) {
      if (this.passwordStore.originalPassword === '' 
      || this.passwordStore.newPassword === '' 
      || this.passwordStore.repeatPassword === '') {
        this.shouldAlert = true
        this.errorMessage = '请输入密码'
      } else if (this.passwordStore.newPassword !== this.passwordStore.repeatPassword) {
        this.shouldAlert = true
        this.errorMessage = '两次密码输入不同'
      } else {
        this.shouldAlert = false;
        this.setAdminPassword(function() {
          if (response.data.status === 1) {
            Ply.dialog("alert", "修改成功！").done(function() {
              window.location.reload();
            });
          } else {
            Ply.dialog("alert", "密码错误，请重试！");
          }  
        });
      }
    },
    setAdminPassword: function(callback) {
      console.log(appAdminSettings.phone);
      console.log(appAdminSettings.passwordStore.newPassword);
      Vue.http
      .post(
        SERVER_PORT + "modifyAdminPwd",
        {
          phone: appAdminSettings.phone,
          password: appAdminSettings.passwordStore.newPassword
        },
        { emulateJSON: true }
      )
      .then(response => {
        callback(response);
      });
    }
  },
  created: function() {
    this.checkAdminPhoneInCookie();
  }
})
